package com.text;

import com.dao.user.BillMapper;
import com.dao.user.ProviderMapper;
import com.entity.Bill;
import com.entity.Provider;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.*;
import java.util.logging.Logger;

public class BillMapperTest {
    private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(UserMapperTest.class);
    public void testSelectBillLike(){
        SqlSession sqlSession = null;
        List<Bill> billList = new ArrayList<Bill>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            String productName = "油";
            billList = sqlSession.getMapper(BillMapper.class).selectBillLike("油");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Bill bill : billList){
            logger.debug("订单编码" + bill.getBillCode() + "商品名称" + bill.getProductName()
                        + "订单金额" + bill.getTotalPrice() + "是否全款" + bill.getIsPayment()
                        + "供应商编码" + bill.getProvider().getProCode() + "供应商名称" + bill.getProvider().getProName()
                        + "供应商联系人" + bill.getProvider().getProContact() + "供应商联系电话" + bill.getProvider().getProPhone());
        }
    }

    public void testBillByConditionMap_foreach_map(){
        SqlSession sqlSession = null;
        List<Bill> billList = new ArrayList<Bill>();
        Map<String,Object> idMap = new HashMap<String, Object>();
        List<Integer> integerList = new ArrayList<Integer>();
        integerList.add(1);
        integerList.add(3);
        idMap.put("billCode","001");
        idMap.put("idMap",integerList);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            billList = sqlSession.getMapper(BillMapper.class).getBillByConditionMap_foreach_map(idMap);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Bill bill : billList){
            logger.debug("订单编码" + bill.getBillCode() + "商品名称" + bill.getProductName()
                    + "订单金额" + bill.getTotalPrice() + "是否全款" + bill.getIsPayment()
                    + "供应商编码" + bill.getProvider().getProCode() + "供应商名称" + bill.getProvider().getProName()
                    + "供应商联系人" + bill.getProvider().getProContact() + "供应商联系电话" + bill.getProvider().getProPhone());
        }
    }
    public void testAddBill(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Bill bill = new Bill();
            bill.setBillCode("BILL2016_019");
            bill.setProductName("123洗洁精");
            bill.setProductDesc("日用品-皂类");
            bill.setProductUnit("瓶");
            bill.setProductCount(100.00);
            bill.setTotalPrice(1000.00);
            bill.setIsPayment(1);
            bill.setCreatedBy(1);
            bill.setCreationDate(new Date());
            bill.setProviderId(13);
            count = sqlSession.getMapper(BillMapper.class).add(bill);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }

    @Test
    public void testUpdateBill(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            Bill bill = new Bill();
            bill.setBillCode("洗洁精");
            bill.setProviderName("123");
            bill.setId(19);
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(BillMapper.class).updateBill(bill);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }
    @Test
    public void selectBillLikeName(){
        SqlSession sqlSession = null;
        List<Bill> billList = new ArrayList<Bill>();
        String productName = "油";
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            billList = sqlSession.getMapper(BillMapper.class).selectLikeNameBill(productName);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Bill bill : billList){
            logger.debug("订单编码" + bill.getBillCode() + "商品名称" + bill.getProductName()
                    + "订单金额" + bill.getTotalPrice() + "是否全款" + bill.getIsPayment()
                    + "供应商编码" + bill.getProvider().getProCode() + "供应商名称" + bill.getProvider().getProName()
                    + "供应商联系人" + bill.getProvider().getProContact() + "供应商联系电话" + bill.getProvider().getProPhone());
        }
    }
    @Test
    public void testProviderId2(){
        SqlSession sqlSession = null;
        List<Provider> providerList = new ArrayList<Provider>();
        Integer id = 3;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            providerList = sqlSession.getMapper(ProviderMapper.class).selectProviderLikeNameCode("001","深圳");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Provider provider : providerList){
            logger.debug("供应商id" + provider.getId() + "供应商编码" + provider.getProCode()
                    + "供应商名称" + provider.getProName() + "供应商联系人" + provider.getProContact()
                    + "供应商联系电话" + provider.getProPhone() + "传真" + provider.getProFax()
                    + "创建时间" + provider.getCreationDate());
        }
    }
    @Test
    public void testProviderListLimit(){
        SqlSession sqlSession = null;
        List<Bill> billList = new ArrayList<Bill>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Integer pageSize = 5;
            Integer currentPageNo = 0;
            billList = sqlSession.getMapper(BillMapper.class).getBillListLimit(currentPageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Bill bill : billList){
            logger.debug("订单编码" + bill.getBillCode() + "商品名称" + bill.getProductName()
                    + "订单金额" + bill.getTotalPrice() + "是否全款" + bill.getIsPayment()
                    + "供应商编码" + bill.getProvider().getProCode() + "供应商名称" + bill.getProvider().getProName()
                    + "供应商联系人" + bill.getProvider().getProContact() + "供应商联系电话" + bill.getProvider().getProPhone());
        }
    }
}
